﻿/**
 * Copyright (C) 2011 original author and authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NGuice.Inject.Internal;

namespace NGuice.Inject
{
    /// <summary>
    /// NGuice框架的入口点，他从Modules模块中创建Injector注入器。NGuice支持能够清晰的描绘API与这些API实现、
    /// 配置了这些实现的模块并最终由这些实现集组成的应用之间的边界的开发模型。这个静态类是整个应用的入口点，
    /// 类似于main主函数，他按照如下方式启动NGuice 的Injector注入器：
    /// <code>
    ///  public class FooApplication {
    ///     public static void main(string[] args) {
    ///         Injector injector = NGuice.CreateInjector(
    ///         new ModuleA(),
    ///         new ModuleB(),
    ///         . . .
    ///         new FooApplicationFlagsModule(args)
    ///         );
    ///        //此时已经启动了应用，你可以获得实例了：
    ///        FooStarter starter = injector.GetInstance(typeof(FooStarter));
    ///        starter.RunApplication();
    ///     }
    ///  }
    /// </code>
    /// </summary>
    public static class NGuice
    {
        /// <summary>
        /// 为给定的模块集创建一个注入器
        /// </summary>
        /// <exception cref="CreationException">在注入器构造器期间发生异常</exception>
        public static Injector CreateInjector(params IModule[] modules)
        {
            return CreateInjector(modules.ToList());
        }

        /// <summary>
        /// 为给定的模块集创建一个注入器
        /// </summary>
        /// <exception cref="CreationException">在注入器构造器期间发生异常</exception>
        public static Injector CreateInjector(IEnumerable<IModule> modules)
        {
            return CreateInjector(Stage.DEVELOPMENT, modules);
        }

        /// <summary>
        /// 在给定的环境下，为给定的模块集创建一个注入器
        /// </summary>
        /// <exception cref="CreationException">在注入器构造器期间发生异常</exception>
        public static Injector CreateInjector(Stage stage, params IModule[] modules)
        {
            return CreateInjector(stage, modules.ToList());
        }

        /// <summary>
        /// 在给定的环境下，为给定的模块集创建一个注入器
        /// </summary>
        /// <exception cref="CreationException">在注入器构造器期间发生异常</exception>
        public static Injector CreateInjector(Stage stage, IEnumerable<IModule> modules)
        {
            return new InternalInjectorCreator().Stage(stage).AddModules(modules).Build();
        }
    }
}
